package edu.guigu.todolist.todo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;

import java.util.List;

import edu.guigu.todolist.R;
import edu.guigu.todolist.commons.LogUtils;
import edu.guigu.todolist.databinding.ActivityTodoBinding;
import edu.guigu.todolist.todo.model.TodoModel;
import edu.guigu.todolist.todo.model.info.TodoInfo;
import edu.guigu.todolist.todo.presenter.TodoPresenter;
import edu.guigu.todolist.todo.presenter.TodoPresenterImpl;
import edu.guigu.todolist.todo.view.TodoAdapter;
import edu.guigu.todolist.todo.view.TodoView;

// MVC
// MVP
// MVVM

public class TodoActivity extends AppCompatActivity implements TodoView {

    private ActivityTodoBinding binding;

    private TodoPresenter presenter;
    private TodoAdapter todoAdapter;

    // private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        // setContentView(R.layout.activity_todo);
        binding = ActivityTodoBinding.inflate(getLayoutInflater());//  inflating 绑定类
        setContentView(binding.getRoot());// 设置根视图
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        presenter = new TodoPresenterImpl(this);
        presenter.loadTodoList();
    }

    @Override
    public void initView() {
        todoAdapter = new TodoAdapter(onItemChildClickLongListener);
        // 设置RecyclerView的Item项动画
        binding.recyclerView.setItemAnimator(new DefaultItemAnimator());
        // 设置RecyclerView的布局管理器
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 设置RecyclerView的适配器
        binding.recyclerView.setAdapter(todoAdapter);

        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.addTodo(new TodoInfo("新的"));
            }
        });
    }

    @Override
    public void showTodoListView(List<TodoInfo> todoInfoList, int type, int position) {
        LogUtils.d("showTodoListView type:" + type + ",position = " + position);
        switch (type) {
            case TYPE_QUEST_DATA:
                todoAdapter.setTodoInfoList(todoInfoList);
                break;
            case TYPE_REQUEST_DATA:
                todoAdapter.setTodoInfoList(todoInfoList);
                todoAdapter.notifyDataSetChanged();
                break;
            case TYPE_ADD_DATA:
                todoAdapter.notifyItemInserted(position);
                break;
            case TYPE_UPDATE_DATA:
                todoAdapter.notifyItemChanged(position);
                break;
            case TYPE_REMOVE_DATA:
                todoAdapter.notifyItemRemoved(position);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    private TodoAdapter.OnItemChildLongClickListener onItemChildClickLongListener = new TodoAdapter.OnItemChildLongClickListener() {
        @Override
        public void onItemChildLongClick(View view, int position, String type) {
            LogUtils.d("onItemChildClick " + position);
            presenter.removeTodo(position);
        }
    };
}